﻿using JewererBasic.Classes;
using JewererBasic.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Transactions;
using System.Web;

namespace JewererBasic.BO
{
    public class MouldBO
    {
        public static bool Add(MOULD mould)
        {
            MSQDBEntities db = new MSQDBEntities();
            var query = from d in db.MOULDs
                        where d.IdMould.Equals(mould.IdMould)
                        select d;
            if (query.Count() > 0)
                return false;
            db.MOULDs.Add(mould);
            try
            {
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                // validation error
                return false;
            }
            return true;
        }
       
        public static bool Update(MOULD mould)
        {
            MSQDBEntities db = new MSQDBEntities();
            var query = db.MOULDs.Find(mould.IdMould);
            if (query != null)
            {
                query.Name = mould.Name;
                try
                {
                    db.SaveChanges();
                    return true;
                }
                catch (Exception ex)
                {
                    return false;
                }
            }
            return false;
        }

        public static List<MOULD> Search(string id, string name)
        {
            MSQDBEntities db = new MSQDBEntities();
            var query = (from d in db.MOULDs
                         where d.IdMould.Contains(id) && d.Name.Contains(name)
                         select d).ToList();
            return query;
        }

        public static List<MOULD> Moulds()
        {
            MSQDBEntities db = new MSQDBEntities();
            return db.MOULDs.ToList();
        }

        public static bool Import(List<MOULD> details)
        {
            using (MSQDBEntities db = new MSQDBEntities())
            {
                // use transaction to prevent error on data.
                using (var scope = new TransactionScope())
                {
                    try
                    {                        
                        // add weight and numb to mould
                        for (int i = 0; i < details.Count; i++)
                        {
                            var query = db.MOULDs.Find(details[i].IdMould);
                            if (query == null)
                                return false;
                            query.Numb += details[i].Numb;
                        }
                        db.SaveChanges();
                        scope.Complete();
                    }
                    catch (Exception e)
                    {
                        return false;
                    }
                }
                return true;
            }
        }

        public static bool Export(List<MOULD> details)
        {
            using (MSQDBEntities db = new MSQDBEntities())
            {
                using (var scope = new TransactionScope())
                {
                    try
                    {                        
                        // subtractmould
                        for (int i = 0; i < details.Count; i++)
                        {
                            var query = db.MOULDs.Find(details[i].IdMould);
                            if (query != null)
                            {
                                query.Numb -= details[i].Numb;
                            }
                        }
                        db.SaveChanges();
                        scope.Complete();
                    }
                    catch (Exception ex)
                    {
                        return false;
                    }
                }
                return true;
            }
        }

        public static List<BAG_MOULD> BagMoulds(string idBag)
        {
            MSQDBEntities db = new MSQDBEntities();
            var query = from d in db.BAG_MOULD
                        where d.IdBag.Equals(idBag)
                        select d;
            return query.ToList();
        }
            
    }
}